package cn.sm1234.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sm1234.domain.Blog;
import cn.sm1234.service.BlogService;

@Controller
@RequestMapping("/blog")
public class BlogController {
	@Autowired
	private BlogService blogService;
	
	/**
	 * 添加
	 */
	@RequestMapping("/save")
	public String save(Blog blog){
		blogService.save(blog);
		return "succ";
	}
	
	/**
	 * 跳转到blog/input.jsp页面
	 */
	@RequestMapping("/input")
	public String input(){
		return "blog/input";
	}
	
	/**
	 * 列表展示
	 */
	@RequestMapping("/list")
	public String list(Model model){
		List<Blog> list = blogService.findAll();
		//把数据存入model，传递到页面
		model.addAttribute("list", list);
		return "blog/list";
	}
	
	/**
	 * 根据id查询
	 */
	@RequestMapping("/findById")
	public String findById(Integer id,Model model){
		Blog blog = blogService.findById(id);
		//把blog对象传回input.jsp
		model.addAttribute("blog", blog);
		return "blog/input";
	}

	/**
	 * 删除博客信息
	 */
	@RequestMapping("/delete")
	public String delete(Integer id){
		blogService.delete(id);
		//跳转到列表页面
		return "redirect:/blog/list";
	}
}
